package com.scsj.supermarket.model;

import com.scsj.supermarket.base.MarketBase;
import com.scsj.supermarket.bean.BaseBean;
import com.scsj.supermarket.bean.MarketCouPounBean;
import com.scsj.supermarket.bean.MarketDetailBean;
import com.scsj.supermarket.bean.MarketIsOpen;
import com.scsj.supermarket.bean.MarketLeftBean;
import com.scsj.supermarket.bean.MarketRightBean;
import com.scsj.supermarket.bean.MarketShopCarBean;
import com.scsj.supermarket.bean.MarketTopBean;
import com.scsj.supermarket.bean.ShopCarBean;

import dkmvp.net.ResponseCallback;
import dkmvp.net.ResponseHandler;
import dkmvp.net.RetryWithDelay;
import okhttp3.RequestBody;
import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 店铺
 */

public class MarketModel extends BaseModel implements MarketBase.IModel {

    /**
     * 店铺内购物车内商品
     *
     * @param requestBody
     * @param callback
     * @return
     */
    @Override
    public Subscription getMarketShopCarData(RequestBody requestBody, final ResponseCallback<MarketShopCarBean> callback) {
        Observable<MarketShopCarBean> observable = service.GetMarketInsideShopCar(requestBody);

        Subscription sub = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .retryWhen(new RetryWithDelay(3, 3000))//总共重试3次，重试间隔3秒
                .subscribe(new Action1<MarketShopCarBean>() {
                    @Override
                    public void call(MarketShopCarBean result) {
                        callback.onSuccess(result);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        callback.onFaild(ResponseHandler.getErrorMsg(throwable));
                    }
                });
        return sub;
    }

    /**
     * 加减店铺购物车中数据
     *
     * @param requestBody
     * @param callback
     * @return
     */
    @Override
    public Subscription getControlMarketShopCarData(RequestBody requestBody, final ResponseCallback<BaseBean> callback) {
        Observable<BaseBean> observable = service.GetMarketShopCarNum(requestBody);

        Subscription sub = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .retryWhen(new RetryWithDelay(3, 3000))//总共重试3次，重试间隔3秒
                .subscribe(new Action1<BaseBean>() {
                    @Override
                    public void call(BaseBean result) {
                        callback.onSuccess(result);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        callback.onFaild(ResponseHandler.getErrorMsg(throwable));
                    }
                });
        return sub;
    }

    /**
     * 店铺内优惠券
     *
     * @param requestBody
     * @param callback
     * @return
     */
    @Override
    public Subscription getMarketCoupounData(RequestBody requestBody, final ResponseCallback<MarketCouPounBean> callback) {
        Observable<MarketCouPounBean> observable = service.GetMarketCoupoun(requestBody);

        Subscription sub = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .retryWhen(new RetryWithDelay(3, 3000))//总共重试3次，重试间隔3秒
                .subscribe(new Action1<MarketCouPounBean>() {
                    @Override
                    public void call(MarketCouPounBean result) {
                        callback.onSuccess(result);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        callback.onFaild(ResponseHandler.getErrorMsg(throwable));
                    }
                });
        return sub;
    }

    /**
     * 获取店铺左侧数据
     * @param requestBody
     * @param callback
     * @return
     */
    @Override
    public Subscription getMarketDetailData(RequestBody requestBody, final ResponseCallback<MarketDetailBean> callback) {
        Observable<MarketDetailBean> observable = service.GetMarketDetail(requestBody);

        Subscription sub = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .retryWhen(new RetryWithDelay(3, 3000))//总共重试3次，重试间隔3秒
                .subscribe(new Action1<MarketDetailBean>() {
                    @Override
                    public void call(MarketDetailBean result) {
                        callback.onSuccess(result);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        callback.onFaild(ResponseHandler.getErrorMsg(throwable));
                    }
                });
        return sub;
    }

    /**
     * 获取右侧店铺数据
     * @param requestBody
     * @param callback
     * @return
     */
    @Override
    public Subscription getMarketRightData(RequestBody requestBody, final ResponseCallback<MarketRightBean> callback) {
        Observable<MarketRightBean> observable = service.GetMarketRightList(requestBody);

        Subscription sub = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .retryWhen(new RetryWithDelay(3, 3000))//总共重试3次，重试间隔3秒
                .subscribe(new Action1<MarketRightBean>() {
                    @Override
                    public void call(MarketRightBean result) {
                        callback.onSuccess(result);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        callback.onFaild(ResponseHandler.getErrorMsg(throwable));
                    }
                });
        return sub;
    }
}
